
var selectedArr = new Array (4);
//像后端请求所有选课数据，后期将这部分内容加入到onload事件中，其中调用了showAllCourse(data) 方法
window.onload = function () {
    $.ajax({
        url: "/RuanJianKeShe/selectCourse",
        type: "post",
        data: {
            "msg":"selectCourse"
        },
        dataType: "json",
        success: function (data) {
            showAllSelectCourse(data.allSelectableCourses);
            console.log(data)
        },
        error: function () {
            alert("fail");
        }
    })
}

showAllSelectCourse = function (data) {
    var str = "";
    var checkBoxId = "";//复选框id
    console.log(data)
    for (var i = 0; i < data.length; i++) {
        selectId = "selectId"+i;
        str = "<tr class='selected' id='selected' >" +
        "<td id='selectedCno'>" + data[i].cno +  "</td>" +
        "<td>" + data[i].cname + "</td>" +
        "<td>" + data[i].address + "</td>" +
        "<td>" + data[i].teachTime + "</td>" +
        "<td>" + data[i].teachWeek + "</td>" +
        "<td>" + data[i].tname + "</td>" +
        "<td>" + data[i].allowance + "</td>" +
        "<td>" + data[i].capacity + "</td>" +
        "<td><button class='theNewBtn' id='checkboxInput' onclick=\"selectCourseHandle(this.value)\" type=\"button\" value='selectId'   name=\"selectCourse\">选择</button></td>" +
        "<td></td>" +
        "</tr>";
        $("#tab").append(str);
        $("#checkboxInput").attr('value',selectId);
        $("#checkboxInput").attr('id',"checkBoxId"+selectId);
        $("#selected").attr('id',selectId);
        $("#selectedCno").attr('id',"selectedCno"+selectId);

    }
}
    //将最终选课结果提交，上传至数据库
$('#addStuCourseBtn').click(function () {
    var result = [];
    for (var i = 0; i <selectedArr.length;i++ ){
        if (selectedArr[i] == null)
            break;
        var cnolist = document.getElementById("selectedCno"+selectedArr[i]).innerText;
        result.push(cnolist)
    }
    var jsonData = {
    "resultList" : result,
    "msg" : "addSelectResultCourse",

    }
    console.log(jsonData);
    JSON.stringify(jsonData);
    console.log(JSON.stringify(jsonData));
    console.log(typeof JSON.stringify(jsonData));
    $.ajax({
        url:"/RuanJianKeShe/addSelectResultCourse",
        type:"post",
        dataType: 'json',
        traditional:true,
        data:{
        "jsonData" :  JSON.stringify(jsonData)
        },
        success:function(data){
            alert(data.info)
        },error: function () {
            window.location.reload();
        }
    })
});
selectCourseHandle = function(value){
    var flag = true;
    for (var i = 0; i < selectedArr.length; i ++){
        if (selectedArr[i]==value){
        selectedArr[i]=null;
        $("#checkBoxId"+value).attr("checked",false);
        var selectedList = document.getElementById("selectedList"+value);
        selectedList.parentNode.removeChild(selectedList);

        flag = false;
        break;
    }
}
    if (flag == true){
        for (var i = 0; i < selectedArr.length; i ++) {
            if (selectedArr[i]==null){
            selectedArr[i] = value;
            var selected = document.getElementById(value);
            showSelectedCourse(selected.innerHTML,value);
            break;
            }
        }
    }
}
    //展示在本页面已选课程
    function showSelectedCourse(data,value) {
    console.log(value)
    var str = "";
    str = "<tr id='selectedList'>"+data+"</tr>";
    $("#selectedTab").append(str);
    $("#selectedList").attr("id","selectedList"+value);
}
